package com.cyys.modules.system.tag;

import com.cyys.common.utils.StringUtil;
import com.cyys.modules.core.sysParam.ParamBasicUtil;
import com.cyys.modules.core.sysParam.ParamConstants;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

public class DictTag extends SimpleTagSupport{

    private String name;

    private String dv;

    private Integer type = 1;

	@Override
    public void doTag() throws JspException, IOException {
	    Map<String, String> map = new LinkedHashMap<>();
        map = ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.getValue(name));
        StringBuffer buffer = new StringBuffer();
        switch (type){
            case 1: buffer = getOption(map);
            case 2: buffer = getOption(map);
            case 3: buffer = getOption(map);
            default:buffer = getOption(map);
        }
        getJspContext().getOut().write(buffer.toString());
    }

    private StringBuffer getOption(Map<String, String> map){
        StringBuffer buffer = new StringBuffer();
        map.forEach((key, value) -> {
            buffer.append("<option value=\"" + key + "\"");
            if(StringUtil.isNotBlank(dv) && dv.equals(key)){
                buffer.append(" selected=\"selected\"");
            }
            buffer.append(">" + value + "</option>");
        });
        return buffer;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDv() {
        return dv;
    }

    public void setDv(String dv) {
        this.dv = dv;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }
}
